part of '../repository.dart';

const _kPaymentRequestAmountKey = 'amount';
const _kPaymentRequestDescriptionKey = 'description';
const _kPaymentRequestCaptureKey = 'capture';
const _kPaymentRequestPaymentMethodIdKey = 'payment_method_id';
const _kPaymentRequestPaymentMethodDataKey = 'payment_method_data';
const _kPaymentRequestSavePaymentMethodKey = 'save_payment_method';
const _kPaymentRequestConfirmationInfoKey = 'confirmation';
const _kPaymentRequestMetadataKey = 'metadata';
const _kPaymentRequestPaymentTokenKey = 'payment_token';

class _PaymentRequestDto extends Equatable {
  const _PaymentRequestDto._internal({
    required this.amountDto,
    required this.description,
    required this.capture,
    required this.yookassaApiPaymentMethodId,
    required this.paymentMethodDataDto,
    required this.savePaymentMethod,
    required this.confirmationInfoDto,
    required this.metadataDto,
    required this.paymentToken,
  });

  final YookassaApiMoneyAmountDto amountDto;
  final String description;
  final bool capture;
  final String? yookassaApiPaymentMethodId;
  final _PaymentRequestPaymentMethodDataDto? paymentMethodDataDto;
  final bool? savePaymentMethod;
  final _PaymentRequestConfirmationInfoDto? confirmationInfoDto;
  final YookassaApiPaymentMetadataDto metadataDto;
  final String? paymentToken;

  @override
  List<Object?> get props {
    return [
      amountDto,
      description,
      capture,
      yookassaApiPaymentMethodId,
      paymentMethodDataDto,
      savePaymentMethod,
      confirmationInfoDto,
      metadataDto,
      paymentToken,
    ];
  }

  factory _PaymentRequestDto.fromEntity(YookassaApiPaymentRequest paymentRequest) {
    final amountDto = YookassaApiMoneyAmountDto.fromEntity(paymentRequest.amount);

    _PaymentRequestPaymentMethodDataDto? paymentMethodDataDto;

    if (paymentRequest.paymentMethodData != null) {
      paymentMethodDataDto = const _PaymentRequestPaymentMethodDataDto();
    }

    _PaymentRequestConfirmationInfoDto? confirmationInfoDto;

    if (paymentRequest.confirmationInfo != null) {
      confirmationInfoDto =
          _PaymentRequestConfirmationInfoDtoFactory().fromEntity(paymentRequest.confirmationInfo!);
    }

    final metadataDto = YookassaApiPaymentMetadataDto(
      purchaseItemSku: paymentRequest.purchaseItemSku,
      userId: paymentRequest.userId,
      savedPaymentMethodId: paymentRequest.savedPaymentMethodId,
    );

    return _PaymentRequestDto._internal(
      amountDto: amountDto,
      description: paymentRequest.description,
      capture: paymentRequest.capture,
      yookassaApiPaymentMethodId: paymentRequest.yookassaApiPaymentMethodId,
      paymentMethodDataDto: paymentMethodDataDto,
      savePaymentMethod: paymentRequest.savePaymentMethod,
      confirmationInfoDto: confirmationInfoDto,
      metadataDto: metadataDto,
      paymentToken: paymentRequest.paymentToken,
    );
  }

  Map<String, dynamic> toJson() {
    final json =  {
      _kPaymentRequestAmountKey: amountDto.toJson(),
      _kPaymentRequestDescriptionKey: description,
      _kPaymentRequestCaptureKey: capture,
      _kPaymentRequestSavePaymentMethodKey: savePaymentMethod,
      _kPaymentRequestConfirmationInfoKey: confirmationInfoDto?.toJson(),
      _kPaymentRequestMetadataKey: metadataDto.toJson(),
    };

    if (yookassaApiPaymentMethodId != null) {
      json[_kPaymentRequestPaymentMethodIdKey] = yookassaApiPaymentMethodId;
    }

    if (paymentMethodDataDto != null) {
      json[_kPaymentRequestPaymentMethodDataKey] = paymentMethodDataDto?.toJson();
    }

    if (paymentToken != null) {
      json[_kPaymentRequestPaymentTokenKey] = paymentToken;
    }

    return json;
  }
}
